package com.xidian.zhiancloud.controller.exam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.exam.TExamPaper;
import com.xidian.zhiancloud.model.exam.TResource;
import com.xidian.zhiancloud.service.exam.ITResourceService;
import com.xidian.zhiancloud.util.tools.FileUploadUtils;
import com.xidian.zhiancloud.util.tools.FileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


/**
 * 文件管理Controller
 * 
 * @author zhouweitao
 * @date 2021-03-10
 */
@RestController
@RequestMapping("/resource")
public class TResourceController 
{
	/**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    @Autowired
    private ITResourceService tResourceService;

    /**
     * 查询文件管理列表
     */
    @GetMapping("/list")
    public ResultDTO list( TResource tResource)
    {
    	Page<TResource> pagedata = tResourceService.selectTResourceList(tResource);
    	 Map<String, Object> data = new HashMap<String, Object>();
 		data.put("datalist", pagedata);
 		data.put("PageNum", pagedata.getPageNum()+1);
 		data.put("PageSize", pagedata.getPageSize());
 		data.put("PageTotal", pagedata.getPages());
 		data.put("total", pagedata.getTotal());
 		return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }
    
    


    /**
     * 获取文件管理详细信息
     */
    @GetMapping(value = "/{id}")
    public ResultDTO getInfo(@PathVariable("id") Long id)
    {
    	return ResultDTO.successOf(tResourceService.selectTResourceById(id),GeneralResponseEnums.SUCCESS);
    }
    
    
    /**
     * 新增文件管理
     * @throws IOException 
     */
    @PostMapping("/addFolder")
    public ResultDTO addFolder(@RequestBody TResource tResource) throws IOException
    {
    	//FileUploadUtils.getAbsoluteFile(localFilePath+File.separator+tResource.getName(),null);
    	tResource.setType("文件夹");
    	tResourceService.insertTResource(tResource);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    
    /**
     * 移动文件
     * @throws IOException 
     */
    @PostMapping("/moveToFolder/{folder}")
    public ResultDTO moveToFolder(@PathVariable("folder") String folder, List<TResource> tResources) throws IOException
    {
    	
    	//FileUploadUtils.getAbsoluteFile(localFilePath+File.separator+tResource.getName(),null);
    	//tResource.setType("文件夹");
    	//tResourceService.insertTResource(tResource);
    	for (int i = 0; i < tResources.size(); i++) {
    		String url =null;
    		if(tResources.get(i).getFolder() !=null && !tResources.get(i).getFolder().equals("")) {
    			url = tResources.get(i).getFolder()+File.separator+tResources.get(i).getName();
    		}else {
    			url =tResources.get(i).getName();
    		}
    		File startFile=new File(localFilePath+File.separator+url);
    		File endDirection=new File(localFilePath+File.separator+folder);
    		FileUtils.moveFolder(startFile, endDirection);
    		tResources.get(i).setFolder(folder);
		}
    	tResourceService.updateBatch(tResources);
    	
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    

    /**
     * 新增文件管理
     */
    @PostMapping
    public ResultDTO add(@RequestBody TResource tResource)
    {
    	 String extension = FilenameUtils.getExtension(tResource.getName());
    	String type = FileUploadUtils.getFileContentType2(extension);
    	tResource.setType(type);
    	tResourceService.insertTResource(tResource);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 修改文件管理
     */
    @PutMapping
    public ResultDTO edit(@RequestBody TResource tResource)
    {
    	tResourceService.updateTResource(tResource);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除文件管理
     */
	@DeleteMapping("/{ids}")
    public ResultDTO remove(@PathVariable Long[] ids)
    {
		tResourceService.deleteTResourceByIds(ids);
		return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
	
	/**
     * 移动文件s
     */
	@PostMapping("/move")
    public ResultDTO move(@Param("parentId")Long parentId, @Param("parentId") Long[] ids )
    {
		tResourceService.move(parentId,ids);
		return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
	
	
	
}
